package com.zhongkai.controller;

import com.zhongkai.app.service.DeviceControlService;
import com.zhongkai.app.service.SetUnitService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class SetUnitController {

    @Autowired
    private SetUnitService setUnitService;


    @Autowired
    private DeviceControlService appRegLogService;

/*    @Autowired
    private LoginInterceptor loginInterceptor;*/


    /**
     * 设备列表
     *
     * @param trus 标识 1已绑定 2已托管  3未绑定
     * @param wire 是否在线 1在线 2离线
     * @param page 当前页数
     * @param rows 每页数据条数
     */
    @RequestMapping(value = "/unit/search_list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_search(
            String trus, String wire, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer rows

    ) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_search(
                trus, wire
                , page
                , rows


        );

        return zhongkaiResult;
    }


    /**
     * 设备列表
     *
     * @param is_assign 是否绑定业主 1是 2否
     * @param page      当前页数
     * @param rows      每页数据条数
     */
    @RequestMapping(value = "/unit/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_list(
            String is_assign, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer rows

    ) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_list(
                is_assign
                , page
                , rows


        );

        return zhongkaiResult;
    }


    /**
     * 修改已绑定用户设备的信息
     *
     * @param uuid         设备uuid
     * @param address      详细地址
     * @param province     省
     * @param city         市
     * @param country      区
     * @param town         乡镇
     * @param longitude    经度
     * @param latitude     纬度
     * @param set_vol_uuid 大客户uuid 传0代表没有
     * @param set_alias    设备别名
     */
    @RequestMapping(value = "/unit/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_update(
            String uuid, String address, String province, String city, String country, String town,
            String longitude, String latitude, String set_vol_uuid, String set_alias
    ) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_update(
                uuid
                , address
                , province
                , city
                , country
                , town
                , longitude
                , latitude
                , set_vol_uuid
                , set_alias


        );

        return zhongkaiResult;
    }


    /**
     * 删除设备
     *
     * @param uuid 设备uuid
     */
    @RequestMapping(value = "/unit/remove", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_remove(
            String uuid
    ) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_remove(
                uuid);


        return zhongkaiResult;
    }


    /**
     * 设备搜索
     *
     * @param name     设备绑定的用户
     * @param province 省
     * @param city     市
     * @param country  区
     * @param town     乡镇
     * @param page     当前页数
     * @param rows     每页数据条数
     */
    @RequestMapping(value = "/unit/userq", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_userq(
            String name, String province, String city, String country, String town, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer rows
    ) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_userQ(
                name,
                province,
                city,
                country,
                town,
                page,
                rows
        );


        return zhongkaiResult;
    }


    /**
     * 已托管设备列表
     *
     * @param page 当前页数
     * @param rows 每页数据条数
     */
    @RequestMapping(value = "/unit/trusteeship", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_trusteeship(
            @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer rows
    ) {


        ZhongkaiResult zhongkaiResult = null;
        try {
            zhongkaiResult = setUnitService.unit_trusteeship(

                    page,
                    rows
            );
        } catch (IOException e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "解析失败");
        }


        return zhongkaiResult;
    }

    /**
     * 设备升级
     *
     * @param list 当前需要升级的设备集合
     */
    @RequestMapping(value = "/unit/upgrade", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult unit_upgrade(
            String list
    ) {
/*        List<UpgradeBean> list = new ArrayList<>();
        for (UpgradeBean bean : beans) {
            list.add(bean);
        }*/
        //System.out.println(new Gson().toJson(list));
        ZhongkaiResult zhongkaiResult = setUnitService.unit_upgrade(
                list
        );


        return zhongkaiResult;
    }

    /**
     * 为设备绑定用户
     *
     * @param unit_uuid    设备uuid
     * @param user_name    用户姓名
     * @param user_phone   用户手机号
     * @param user_number  身份证号
     * @param sex          性别 1男 2女
     * @param longitude    经度
     * @param latitude     纬度
     * @param province     省
     * @param city         市
     * @param country      区
     * @param town         乡镇
     * @param address      详细地址
     * @param pact_num     合同编号
     * @param set_vol_uuid 大客户uuid 传0代表没有
     * @param set_alias    设备别名
     */
    @RequestMapping(value = "/unit/bound", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult bound_user(
            String unit_uuid, String user_name, String user_phone, String user_number, String sex, String longitude, String latitude, String province, String city,
            String country, String town, String address, String pact_num, String set_vol_uuid, String set_alias
            , HttpServletRequest httpServletRequest
    ) {


        Chinaset chinaset = (Chinaset) httpServletRequest.getAttribute("user");
        ZhongkaiResult zhongkaiResult = setUnitService.bound_user(unit_uuid, user_name, user_phone, user_number, sex, longitude, latitude, province, city, country, town, address, chinaset.getSetName(), chinaset.getUuid(), pact_num, set_vol_uuid, set_alias);


        return zhongkaiResult;
    }


    @RequestMapping(value = "/unit/versions_code", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult versions_code() {


        ZhongkaiResult zhongkaiResult = setUnitService.versions_code();

        return zhongkaiResult;
    }


    /**
     * 区域升级
     *
     * @param area   区域标识 1省 2市 3区 4乡镇
     * @param areaId 区域id
     */
    @RequestMapping(value = "/unit/upgrade_area", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult upgrade_area(String area, String areaId) {


        ZhongkaiResult zhongkaiResult = setUnitService.unit_area(area, areaId);

        return zhongkaiResult;
    }


    /**
     * 大客户列表
     */
    @RequestMapping(value = "/unit/big_user_list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getBigUserList() {


        ZhongkaiResult zhongkaiResult = setUnitService.getBigUserList();

        return zhongkaiResult;
    }


    /**
     * 设备托管
     *
     * @param unit_uuid   设备uuid
     * @param trusteeship 托管参数 1取消托管  2托管
     */
    @RequestMapping(value = "/unitrusteeship", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult uniTrusteeship(String unit_uuid, String trusteeship) {


        ZhongkaiResult zhongkaiResult = setUnitService.uniTrusteeship(unit_uuid, trusteeship);

        return zhongkaiResult;
    }



    /***
     * 解除绑定
     *
     * @param unit_uuid  设备uuid
     * @param user_uuid  用户uuid
     * */
    @RequestMapping(value = "/unit/relieve", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult relieveUnit(String unit_uuid,String user_uuid) {


        ZhongkaiResult zhongkaiResult = setUnitService.relieveUnit(unit_uuid, user_uuid);

        return zhongkaiResult;
    }


}
